package cn.hackcloud.blog.controller.sys;


import cn.hackcloud.blog.dao.sys.impl.UserServiceImpl;
import cn.hackcloud.blog.domain.sys.User;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hackcloud
 * @since 2020-03-13
 */
@RestController
@RequestMapping("/sys/user")
public class UserController {
    UserServiceImpl userService;

    @Autowired
    public void setUserService(UserServiceImpl userService) {
        this.userService = userService;
    }

    @GetMapping("/list")
    @ResponseBody
    public String getUserList() {
        List<User> list = userService.list();
        return JSON.toJSONString(list);
    }

    @GetMapping("/add")
    @ResponseBody
    public String add() {
        User user = new User();
        user.setNickName("宇宙脉冲");
        user.setAccount("hackcloud");
        user.setEmail("m@hackcloud.cn");
        user.setPassword("1111225");
        user.setStatus("1");
        userService.save(user);
        return JSON.toJSONString(user);
    }

    @GetMapping("/update")
    @ResponseBody
    public String update() {
        User user = userService.getById(1);
        user.setNickName("雷神");
        userService.saveOrUpdate(user);
        return JSON.toJSONString(user);
    }
    @GetMapping("/insertBatch")
    @ResponseBody
    public String insertBatch() {
        User user = new User();
        user.setNickName("宇宙脉冲");
        user.setAccount("hackcloud");
        user.setEmail("m@hackcloud.cn");
        user.setPassword("1111225");
        user.setStatus("1");
        user.setDate(new Date());
        userService.getBaseMapper().insertBatch(Arrays.asList(user));
        return JSON.toJSONString(user);
    }
    @GetMapping("/del")
    @ResponseBody
    public String del() {
        boolean user = userService.removeById(1);
        return JSON.toJSONString(user);
    }
}

